import { ref, onMounted, onUnmounted } from "vue"
import { loadMicroApp, } from "qiankun"
export default (option) => {
    let { name, entry, props = {} } = option
    if (!(name && entry)) {
        throw "请传入name和entry"
    }
    let refDom = ref(null)
    let app = ref(null)
    onMounted(() => {
        app.value = loadMicroApp({
            container: refDom.value,
            name, entry, props
        })
    })
    onUnmounted(() => {
        if (app.value) {
            app.value.unmount()
        }
    })
    function update(props) {
        if (app.value) {
            app.value.update = app.value.update || function () {
                throw "请在子系统中添加对应的update函数"
            }
            app.value.update(props)
        }
    }
    return {
        refDom,
        app,
        update
    }
}